package com.app.couponsharelife.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.databinding.LocalShopAddressSelectContentCurrentItemBinding
import com.app.couponsharelife.databinding.LocalShopAddressSelectContentHotItemBinding
import com.app.couponsharelife.databinding.LocalShopAddressSelectContentItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

class LocalShopAddressSelectContentAdapter(val list: MutableList<Any>,
                                           private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

//    init {
//        for (i in 0..25) list.add(AppUtils.asciiToString((65 + i).toString()))
//    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            1 -> ViewHolderCurrent(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_address_select_content_current_item, parent, false))
            2 -> ViewHolderHot(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_address_select_content_hot_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_address_select_content_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return when {
            list[position] is PageTipBean -> 0
            list[position] is Int -> list[position] as Int
            else -> 3
        }
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        when {
            getItemViewType(position) == 0 -> (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
            getItemViewType(position) == 1 -> {

            }
            getItemViewType(position) == 2 -> {
                (holder as ViewHolderHot).databinding.mHotCity.removeAllViews()
                val hotList = arrayListOf("杭州")
                val inflater = LayoutInflater.from(holder.itemView.context)
                for (hot in hotList) {
                    val view = inflater.inflate(R.layout.local_shop_address_select_content_hot_list_item, holder.databinding.mHotCity, false) as TextView
                    view.text = hot
                    holder.databinding.mHotCity.addView(view)
                }
                holder.databinding.mHotCity.setListener { position, view ->
                    listener.click(0, hotList[position])
                }
            }
            else -> {
                (holder as ViewHolder).databinding.mRecyclerView.layoutManager = LinearLayoutManager(holder.itemView.context)
                holder.databinding.mRecyclerView.adapter = HistoryRecordListAdapter(arrayListOf("鞍山", "安庆", "安阳", "安康"), object : OnClickListener {
                    override fun click(arg1: Int, arg2: Any) {

                    }
                })
                holder.databinding.tvCharLocationTitle.text = list[position] as String
            }
        }
    }

    inner class ViewHolderCurrent(val databinding: LocalShopAddressSelectContentCurrentItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(adapterPosition, list[adapterPosition])
                }
            }
        }
    }

    inner class ViewHolderHot(val databinding: LocalShopAddressSelectContentHotItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(adapterPosition, list[adapterPosition])
                }
            }
        }
    }

    inner class ViewHolder(val databinding: LocalShopAddressSelectContentItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(adapterPosition, list[adapterPosition])
                }
            }
        }
    }
}